// const RedisBase = require('../common/RedisBase');
const redis = require('../common/Redis')
const game_config = require('../config/game_config');
const log = require('../utils/log');
const IoRedis = require("ioredis");


class gameRedis extends redis {
    static getInstance() {
        if (!gameRedis.instance) {
            gameRedis.instance = new gameRedis();
            return gameRedis.instance;
        } else {
            return gameRedis.instance;
        }
    }

    constructor() {
        super(game_config.center_redis.host, game_config.center_redis.port, game_config.center_redis.db_index);
        log.info('=======gameRedis=constructor=========');
        // this.connectRedis(game_config.center_redis.host, game_config.center_redis.port, game_config.center_redis.db_index);
    }

    //在列表gameInfo_list_zt 中 查找，根据uid 和 type
    async get_redes_in_gameInfo_list_zt_info(uid, type) {
        let key = 'gameInfo_list_zt';

        let list = await this.lrange(key, 0, -1);
        log.error("========get_redes_in_gameInfo_list_zt_info=======", list.length);
        let res = null;
        for (let i = 0; i < list.length; i++) {
            let cfg = JSON.parse(list[i]);
            if (cfg.gamType === type && cfg.uid === uid) {
                res = cfg;
                break;
            }
        }
        return res;
    }

    //已经答对的题目
    async get_redis_topic_list(uid) {
        let key = 'topic_list_' + uid;
        let list = await this.lrange(key, 0, -1);
        return list;
    }
    async push_topic_to_list(uid, tid) {
        let key = 'topic_list_' + uid;
        let res = await this.lpush(key, tid)
        return res;
    }

    //挑战赛的
    async get_redis_topic_list_tz(uid) {
        let key = 'topic_list_tz_' + uid;
        let list = await this.lrange(key, 0, -1);
        return list;
    }
    async push_topic_to_list_tz(uid, tid) {
        let key = 'topic_list_tz_' + uid;
        let res = await this.lpush(key, tid)
        return res;
    }
    // end

    //通过房间号查询对应的连个用户的uid
    async get_redis_in_room_list_zt_by_roomid(roomId) {
        let key = 'room_list_zts';
        let list = await this.lrange(key, 0, -1);

        let res = null;
        for (let i = 0; i < list.length; i++) {
            let cfg = JSON.parse(list[i]);
            if (cfg.roomId === roomId) {
                res = cfg;
                break;
            }
        }
        return res;
    }

    //保存自己的答题数据
    async saveMyRecord(uid,data) {
        let key = 'record_list_zt_' + uid;
        let res = await this.lpush(key, data);
        return res;
    }
    //获取自己的答题记录
    async getMyRecordList(uid) {
        let key = 'record_list_zt_'+uid;
        let list = await this.lrange(key, 0, -1);
        return list;
    }
    //清空答题记录
    async clearMyRecord(uid,data) {
        let key = 'record_list_zt_' + uid;
        let res = await this.del(key);
        return res;
    }
}

global.gameSerMgr.gameRedis = gameRedis.getInstance();
module.exports = gameRedis;